package demo;



public class LinkedList<E> {
    private Node head;
    private int size;


    class Node{
        E data;
        Node next;
        Node prev;
        Node(E e){
            data=e;
        }
    }
    public boolean add(E e){
        Node node=new Node(e);
        if (head==null){
            head=node;
            head.next=head;
            head.prev=head;
            size++;
            return true;
        }
        Node last=head.prev;
        last.next=node;
        node.next=head;
        head.prev=node;
        node.prev=last;
        size++;
        return true;

    }

    @Override
    public String toString() {
        if (head==null){
            return "[]";
        }
        StringBuilder str=new StringBuilder("[");
        str.append(head.data);
        Node node=head.next;
        while(node!=head){
            str.append(","+node.data);
            node=node.next;
        }
        return str.append("]").toString();
    }
    public int size(){
        return size;
    }
}
